package com.tuanzi.loan.business.repository.settlement.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.*;

import java.util.Date;
import java.util.List;

import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Record2;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.entity.settlement.FirstpartyDataChangeRecord;
import com.tuanzi.loan.business.entity.settlement.JfSltBackData;
import com.tuanzi.loan.business.entity.settlement.SettlementUvRecord;
import com.tuanzi.loan.business.repository.settlement.custom.JfBackDataRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.response.settlement.SettlementUvReportResponse;

@Repository
public class JfBackDataRepositoryImpl extends JOOQBaseRepository implements JfBackDataRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return JF_SLT_BACK_DATA;
    }

    @Override
    public List<JfSltBackData> getJfBackListInCurrWeekByType(Integer type, String bdate, String edate,Long companyId) {
        fields(JF_SLT_BACK_DATA.fields());
        eq(JF_SLT_BACK_DATA.TYPE,type);
        eq(JF_SLT_BACK_DATA.COMPANY_ID,companyId);
        ge(JF_SLT_BACK_DATA.BEGIN_DATE,bdate);
        le(JF_SLT_BACK_DATA.BEGIN_DATE,edate);

        return search(new JfSltBackDataMapper()) ;
    }

    @Override
    public List<JfSltBackData> getJfBackListInCurrWeekByCompanyId(Long companyId, String bdate, String edate) {
        fields(JF_SLT_BACK_DATA.fields());
        eq(JF_SLT_BACK_DATA.COMPANY_ID,companyId);
        ge(JF_SLT_BACK_DATA.BEGIN_DATE,bdate);
        le(JF_SLT_BACK_DATA.BEGIN_DATE,edate);

        return search(new JfSltBackDataMapper()) ;
    }

    @Override
    public List<FirstpartyDataChangeRecord> findBySettlementTimeAndCompanyId(Date settlementStartTime, Date settlementEndTime, Long companyId) {
        fields(JF_SLT_BACK_DATA.ID,
                JF_SLT_BACK_DATA.BEGIN_DATE,
                JF_SLT_BACK_DATA.END_DATE,
                JF_SLT_BACK_DATA.MARK_CHANNEL,
                JF_SLT_BACK_DATA.REGISTER_NUM,
                JF_SLT_BACK_DATA.CLICK_NUM,
                JF_SLT_BACK_DATA.LEND_MONEY,
                JF_SLT_BACK_DATA.SLT_MONEY,
                JF_SLT_BACK_DATA.CREATE_MAN,
                JF_SLT_BACK_DATA.CREATE_TIME,
                JF_SLT_BACK_DATA.COMPANY_ID,
                JF_SLT_BACK_DATA.COP_MODE,
                JF_SLT_BACK_DATA.TYPE,
                LOAN_COMPANY_INFO.SETTLEMENT_PRICE_CPS,
                LOAN_COMPANY_INFO.SETTLEMENT_PRICE_CPA,
                LOAN_COMPANY_INFO.COMPANY_NAME,
                LOAN_COMPANY_INFO.PRODUCT_NAME);
        leftJoin(LOAN_COMPANY_INFO,JF_SLT_BACK_DATA.COMPANY_ID.eq(LOAN_COMPANY_INFO.ID));
        le(JF_SLT_BACK_DATA.BEGIN_DATE,settlementEndTime);
        ge(JF_SLT_BACK_DATA.END_DATE,settlementStartTime);
        eq(JF_SLT_BACK_DATA.COMPANY_ID,companyId);
//        eq(LOAN_COMPANY_INFO.PRODUCT_NAME,productName);
        orderBy(JF_SLT_BACK_DATA.BEGIN_DATE, Sort.Direction.ASC);

        return search(new FirstpartyDataChangeRecordDataMapper());
    }


    /**
     * 查询公司所有产品参与uv
     * @param beginDate
     * @param endDate
     * @param proIds
     * @return
     */
    @Override
    public Integer countCompanyProductClickUv(Date beginDate,Date endDate, List<Long> proIds) {
        Condition createDateCondition = null;
        if (beginDate.compareTo(endDate)==0){
            createDateCondition = LOAN_PRODUCT_CLICK_UV.CREATEDATE.eq(new java.sql.Date(beginDate.getTime()));
        }
        else {
            createDateCondition = LOAN_PRODUCT_CLICK_UV.CREATEDATE.between(new java.sql.Date(beginDate.getTime()),new java.sql.Date(endDate.getTime()));
        }
        List<Integer> integers = dslContext.select(LOAN_PRODUCT_CLICK_UV.PHONEID.countDistinct()).from(LOAN_PRODUCT_CLICK_UV)
                .where(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID.in(proIds),
                        createDateCondition)
                .fetchInto(Integer.class);
        if (integers.size()>0){
            return integers.get(0);
        }

        return 0;
    }

    /**
     * 查询每个产品参与uv
     * @param beginDate
     * @param endDate
     * @param proIds
     * @return
     */
    @Override
    public  List<SettlementUvRecord> countPerProductClickUv(Date beginDate,Date endDate, List<Long> proIds) {
        Condition createDateCondition = null;
        if (beginDate.compareTo(endDate)==0){
            createDateCondition = LOAN_PRODUCT_CLICK_UV.CREATEDATE.eq(new java.sql.Date(beginDate.getTime()));
        }
        else {
            createDateCondition = LOAN_PRODUCT_CLICK_UV.CREATEDATE.between(new java.sql.Date(beginDate.getTime()),new java.sql.Date(endDate.getTime()));
        }
        List<SettlementUvRecord> settlementUvRecordList = dslContext.select(LOAN_PRODUCT_CLICK_UV.PHONEID.countDistinct().as("participateUv"),
                LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID.as("productId")).from(LOAN_PRODUCT_CLICK_UV)
                .where(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID.in(proIds),
                        createDateCondition)
                .groupBy(LOAN_PRODUCT_CLICK_UV.ACTIVITY_ID)
                .fetch(new RecordMapper<Record2<Integer, Long>, SettlementUvRecord>() {
                    @Override
                    public SettlementUvRecord map(Record2<Integer, Long> record3) {
                        SettlementUvRecord settlementUvRecord = new SettlementUvRecord();
                        settlementUvRecord.setParticipateUv(record3.get(0,Integer.class));
                        settlementUvRecord.setProductId(record3.get(1,Long.class));
                        return settlementUvRecord;
                    }
                });


        return settlementUvRecordList;
    }





    private final class UvReportDataMapper implements RecordMapper<Record,SettlementUvReportResponse>{

        @Override
        public SettlementUvReportResponse map(Record record) {
            SettlementUvReportResponse response = new SettlementUvReportResponse();
            response.setId(record.get(JF_SLT_BACK_DATA.ID));
            response.setBeginDate(record.get(JF_SLT_BACK_DATA.BEGIN_DATE));
            response.setEndDate(record.get(JF_SLT_BACK_DATA.END_DATE));
            response.setProductName(record.get(LOAN_PRODUCT_INFO.NAME));
            response.setParticipateUv(record.get(JF_SLT_BACK_DATA.PARTICIPATE_UV));
            response.setUvValue(record.get(JF_SLT_BACK_DATA.UV_VALUE));
            return response;
        }
    }

    private final class FirstpartyDataChangeRecordDataMapper implements RecordMapper<Record,FirstpartyDataChangeRecord>{

        @Override
        public FirstpartyDataChangeRecord map(Record record) {
            FirstpartyDataChangeRecord firstpartyDataChangeRecord = new FirstpartyDataChangeRecord();
            firstpartyDataChangeRecord.setSettlementStartTime(record.get(JF_SLT_BACK_DATA.BEGIN_DATE));
            firstpartyDataChangeRecord.setSettlementEndTime(record.get(JF_SLT_BACK_DATA.END_DATE));
            firstpartyDataChangeRecord.setOldSpreadChannel(record.get(JF_SLT_BACK_DATA.MARK_CHANNEL));
            firstpartyDataChangeRecord.setOldRegisterCount(record.get(JF_SLT_BACK_DATA.REGISTER_NUM));
            firstpartyDataChangeRecord.setOldClickCount(record.get(JF_SLT_BACK_DATA.CLICK_NUM));
            firstpartyDataChangeRecord.setOldLoanAmount(record.get(JF_SLT_BACK_DATA.LEND_MONEY));
            firstpartyDataChangeRecord.setOldSettlementAmount(record.get(JF_SLT_BACK_DATA.SLT_MONEY));
            firstpartyDataChangeRecord.setFirstpartyCreateTime(record.get(JF_SLT_BACK_DATA.CREATE_TIME));
            firstpartyDataChangeRecord.setFirstpartyCreator(record.get(JF_SLT_BACK_DATA.CREATE_MAN));
            firstpartyDataChangeRecord.setCompanyId(record.get(JF_SLT_BACK_DATA.COMPANY_ID));
            firstpartyDataChangeRecord.setCopMode(record.get(JF_SLT_BACK_DATA.COP_MODE));
            firstpartyDataChangeRecord.setSettlementType(record.get(JF_SLT_BACK_DATA.TYPE));
            firstpartyDataChangeRecord.setSettlementPercentPrice(record.get(LOAN_COMPANY_INFO.SETTLEMENT_PRICE_CPS));
            firstpartyDataChangeRecord.setSettlementYuanPrice(record.get(LOAN_COMPANY_INFO.SETTLEMENT_PRICE_CPA));
            firstpartyDataChangeRecord.setJiafangId(record.get(JF_SLT_BACK_DATA.ID));
            firstpartyDataChangeRecord.setCompanyName(record.get(LOAN_COMPANY_INFO.COMPANY_NAME));
            firstpartyDataChangeRecord.setProductName(record.get(LOAN_COMPANY_INFO.PRODUCT_NAME));
            return firstpartyDataChangeRecord;
        }
    }

    private final class JfSltBackDataMapper implements RecordMapper<Record,JfSltBackData>{
        @Override
        public JfSltBackData map(Record record) {
            JfSltBackData jfSltBackData = new JfSltBackData();
            jfSltBackData.setId(record.get(JF_SLT_BACK_DATA.ID));
            jfSltBackData.setBeginDate(record.get(JF_SLT_BACK_DATA.BEGIN_DATE));
            jfSltBackData.setEndDate(record.get(JF_SLT_BACK_DATA.END_DATE));
            jfSltBackData.setMarkChannel(record.get(JF_SLT_BACK_DATA.MARK_CHANNEL));
            jfSltBackData.setRegisterNum(record.get(JF_SLT_BACK_DATA.REGISTER_NUM));
            jfSltBackData.setClickNum(record.get(JF_SLT_BACK_DATA.CLICK_NUM));
            jfSltBackData.setLendMoney(record.get(JF_SLT_BACK_DATA.LEND_MONEY));
            jfSltBackData.setSltMoney(record.get(JF_SLT_BACK_DATA.SLT_MONEY));
            jfSltBackData.setCreateTime(record.get(JF_SLT_BACK_DATA.CREATE_TIME));
            jfSltBackData.setCreateMan(record.get(JF_SLT_BACK_DATA.CREATE_MAN));
            jfSltBackData.setRemark(record.get(JF_SLT_BACK_DATA.REMARK));
            jfSltBackData.setCompanyId(record.get(JF_SLT_BACK_DATA.COMPANY_ID));
            jfSltBackData.setType(record.get(JF_SLT_BACK_DATA.TYPE));
            jfSltBackData.setIsEdit(record.get(JF_SLT_BACK_DATA.ISEDIT));
            jfSltBackData.setPzUrl(record.get(JF_SLT_BACK_DATA.PZ_URL));
            jfSltBackData.setCopMode(record.get(JF_SLT_BACK_DATA.COP_MODE));
            return jfSltBackData;
        }
    }
}
